package com.atguigu.udmp.stream.extractor.db;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.spring.PropertyPreFilters;
import com.atguigu.udmp.stream.common.constant.UdmpConst;
import com.atguigu.udmp.stream.extractor.UserEventExtractor;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.Date;


/*{
        {
  "database": "gmall_2023_new",
  "table": "order_detail",
  "type": "insert",
  "ts": 1698217943,
  "xid": 19189,
  "commit": true,
  "data": {
    "id": 14257981,
    "order_id": 54260,
    "sku_id": 35,
    "sku_name": "华为智慧屏V65i 65英寸 HEGE-560B 4K全面屏智能电视机 多方视频通话 AI升降摄像头 4GB+32GB 星际黑",
    "img_url": null,
    "order_price": 5499.00,
    "sku_num": 1,
    "create_time": "2023-10-31 15:12:23",
    "split_total_amount": 5499.00,
    "split_activity_amount": 0.00,
    "split_coupon_amount": 0.00,
    "operate_time": null
    "user_id":121
    "order_status": "1001"
  }
}
}*/
public class OrderUserEventExtractor extends UserEventExtractor {
    @Override
    public boolean match(JSONObject dataJsonObj) {
        if( dataJsonObj.getString("table").equals("order_detail")
        && dataJsonObj.getJSONObject("data").getString("order_status").equals(UdmpConst.GMALL_ORDER_STATUS_UNPAID)){
            return true;
        }
        return false;
    }

    @Override
    public String extractDt(JSONObject dataJsonObj) {
        String createTime = dataJsonObj.getJSONObject("data").getString("create_time");
        String dt = createTime.substring(0,10).replace("-","");
        return dt;
    }

    @Override
    public String extractEventTime(JSONObject dataJsonObj) {
        Date createTime = dataJsonObj.getJSONObject("data").getDate("create_time");
        String eventTime = DateFormatUtils.format(createTime, "yyyy-MM-dd HH:mm:ss");
        return  eventTime;
    }

    @Override
    public Long extractUserId(JSONObject dataJsonObj) {
        return dataJsonObj.getJSONObject("data").getLong("user_id");
    }

    @Override
    public String extractEventInfoJson(JSONObject dataJsonObj) {
        PropertyPreFilters.MySimplePropertyPreFilter preFilter = new PropertyPreFilters().addFilter("order_status", "order_price", "sku_id", "split_total_amount", "sku_num", "create_time", "operate_time");
        String infoJson = JSON.toJSONString(dataJsonObj.getJSONObject("data"), preFilter);
        return infoJson;
    }


    //id
    @Override
    public String extractEventRecordKey(JSONObject dataJsonObj) {
        String id = dataJsonObj.getJSONObject("data").getString("id") ;
        return id;
    }
}
